<template>
  <div class="login-container">
    <div class="header">
      <div class="arrow-with-text-container" >
        <div  @click="fanhui">
          <i class="el-icon-arrow-left back-icon"></i>
          <span class="back-text back-icon">返回</span>
        </div>

      </div>
    </div>


    <!-- 登录表单 -->
    <el-row type="flex" justify="center" align="middle" class="login-wrapper">
      <el-col :span="24" class="form-container">
        <div class="login-title">登录</div>

        <el-form :model="loginForm" :rules="rules" ref="loginForm">
          <!-- 邮箱输入 -->
          <el-form-item prop="email">
            <el-input
                v-model="loginForm.email"
                placeholder="邮箱"
                prefix-icon="el-icon-message"
            ></el-input>
          </el-form-item>

          <!-- 密码输入 -->
          <el-form-item prop="password">
            <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="密码"
                prefix-icon="el-icon-lock"
            ></el-input>
          </el-form-item>

          <!-- 忘记密码链接 -->
          <div class="forgot-password">
            <el-link type="primary" @click="handleForgotPassword" style="color: #606266">忘记密码？</el-link>
          </div>

          <!-- 登录按钮 -->
          <el-button
              type="primary"
              class="login-btn"
              @click="handleLogin"
          >登录</el-button>
        </el-form>

        <!-- 注册提示 -->
        <div class="register-tip">
          还没有账号？
          <el-link type="primary" @click="handleRegister" style="color: #42b983">注册</el-link>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: 'LoginView',
  data() {
    return {
      loginForm: {
        email: '',
        password: ''
      },
      rules: {
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {

  },
  methods: {
    handleLogin() {
      axios.get(`http://localhost:6001/user/login?email=${this.loginForm.email}&password=${this.loginForm.password}`).then(res => {
        if (res.data.message=="登陆失败"){
          this.$message.error(res.data.message)
        }else {
          sessionStorage.setItem('user',JSON.stringify(res.data.data.user))
          this.$router.push('/homePage')
          this.$message.success("欢迎来到主页！"+res.data.data.user.username)

        }

      })
    },
    handleForgotPassword() {
      console.log('跳转忘记密码页面')
    },
    handleRegister() {
     this.$router.push('/zhuce')
    },
    fanhui(){
      this.$router.go(-1)
    }

  }
}
</script>

<style scoped>
.back-btn {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 100;

}
.login-container {
  height: 100vh;
  background-color: #f5f7fa;
}

.time-display {
  position: absolute;
  top: 20px;
  right: 20px;
  font-size: 24px;
  color: #606266;
}

.login-wrapper {
  height: calc(100vh - 80px);
}



.login-title {
  font-size: 24px;
  color: #303133;
  text-align: center;
  margin-bottom: 30px;
  font-weight: 500;
}

.forgot-password {
  text-align: right;
  margin-bottom: 20px;
}

.login-btn {
    background-color: #42b983;
  width: 100%;
  margin-bottom: 40px; /* 调整登录按钮下方的间距 */
  height: 40px;
}

.register-tip {
  text-align: center;
  color: #909399;
}

.el-link {
  vertical-align: baseline;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .form-container {
    padding: 20px;
    margin: 0 15px;
  }

  .login-title {
    font-size: 20px;
    margin-bottom: 20px;
  }.arrow-with-text-container {
     display: flex;
     align-items: center;
     /* 让元素左对齐 */
     justify-content: flex-start;
   }

  .back-icon {
    margin-right: 5px;
    cursor: pointer;
    font-size: 18px;
    color: #00c6ff;
  }

  .back-text {
    cursor: pointer;
  }
}
</style>
